package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.BikeOrderDetailBean;
import com.ekuaitu.kuaitu.map.GeocodeSearchViewUtils;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.KuaituDialog;
import com.ekuaitu.kuaitu.utils.TimeUtils;
import com.ekuaitu.kuaitu.utils.StatusBarUtil;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BikeOrderDetailActivity extends BaseActivity {

    @BindView(R.id._detail_order_bike_return)
    ImageView DetailOrderBikeReturn;
    @BindView(R.id.detail_order_bike_phone)
    ImageView detailOrderBikePhone;
    @BindView(R.id.detail_order_toolbar_bike)
    RelativeLayout detailOrderToolbarBike;
    @BindView(R.id.detail_order_bike_money)
    TextView detailOrderBikeMoney;
    @BindView(R.id.detail_order_bike)
    TextView detailOrderBike;
    @BindView(R.id.detail_order_bike_id)
    TextView detailOrderBikeId;
    @BindView(R.id.detail_order_bike_c)
    TextView detailOrderBikeC;
    @BindView(R.id.detail_order_bike_num)
    TextView detailOrderBikeNum;
    @BindView(R.id.detail_order_bike_time0)
    TextView detailOrderBikeTime0;
    @BindView(R.id.detail_order_bike_tv_haoshi)
    TextView detailOrderBikeTvHaoshi;
    @BindView(R.id.detail_order_bike_iv_haoshi)
    ImageView detailOrderBikeIvHaoshi;
    @BindView(R.id.detail_order_bike_rl)
    AutoRelativeLayout detailOrderBikeRl;
    @BindView(R.id.detail_order_bike_orderTime)
    TextView detailOrderBikeOrderTime;
    @BindView(R.id.detail_order_bike_startTime)
    TextView detailOrderBikeStartTime;
    @BindView(R.id.detail_order_bike_startLocation)
    TextView detailOrderBikeStartLocation;
    @BindView(R.id.detail_order_bike_endTime)
    TextView detailOrderBikeEndTime;
    @BindView(R.id.detail_order_bike_endLocation)
    TextView detailOrderBikeEndLocation;
    @BindView(R.id.detail_order_bike_rl_haoshi)
    AutoLinearLayout detailOrderBikeRlHaoshi;
    @BindView(R.id.detail_order_bike_money2)
    TextView detailOrderBikeMoney2;
    @BindView(R.id.detail_order_bike_coupon)
    TextView detailOrderBikeCoupon;
    @BindView(R.id.detail_order_bike_tv_coupon)
    TextView detailOrderBikeTvCoupon;
    @BindView(R.id.detail_order_bike_coupon_rl)
    AutoRelativeLayout detailOrderBikeCouponRl;
    @BindView(R.id.detail_order_bike_tan)
    TextView detailOrderBikeTan;
    @BindView(R.id.detail_order_bike_sv)
    ScrollView detailOrderBikeSv;
    @BindView(R.id.detail_progressBar_bike_settlement)
    RelativeLayout detailProgressBarBikeSettlement;
    @BindView(R.id.detail_order_bike_tv_money)
    TextView detailOrderBikeTvMoney;
    @BindView(R.id.datail_order_bike_zffs)
    TextView datailOrderBikeZffs;
    private int flag = 0;
    private String orderId;
    private Context mContext = this;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bike_order_detail;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimaryWhite), 0,true);
        Intent intent = getIntent();
        orderId = intent.getStringExtra(IntentKey.INTENT_ORDER_ID);
        getBikeOrderDetail();
    }

    private void getBikeOrderDetail() {
        detailProgressBarBikeSettlement.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getBikeDetail(MyApplication.getInstance().getRequestFieldMap(),orderId, "2").enqueue(new Callback<BikeOrderDetailBean>() {
            @Override
            public void onResponse(Call<BikeOrderDetailBean> call, Response<BikeOrderDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        DecimalFormat df = new DecimalFormat("0.00");
                        detailProgressBarBikeSettlement.setVisibility(View.GONE);
                        BikeOrderDetailBean item = response.body();
                        detailOrderBikeTvMoney.setText(df.format(item.getAttachment().getOrderFavourableModel().getRealPrice()) + "元");
                        detailOrderBikeId.setText(item.getAttachment().getOrderFavourableModel().getOrderNum());
                        detailOrderBikeNum.setText(item.getAttachment().getOrderFavourableModel().getBikeModel().getLicenseTag());
                        detailOrderBikeTvHaoshi.setText(TimeUtils.formatMinPlusOne((item.getAttachment().getOrderFavourableModel().getEndBillingTime() - item.getAttachment().getOrderFavourableModel().getStartBillingTime()) / 1000 + ""));
                        detailOrderBikeOrderTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getOrderTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        detailOrderBikeStartTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getStartBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        detailOrderBikeEndTime.setText(TimeUtils.getFormatDate(item.getAttachment().getOrderFavourableModel().getEndBillingTime() + "", "yyyy-MM-dd HH:mm:ss"));
                        GeocodeSearchViewUtils geocodeSearchViewUtils1 = new GeocodeSearchViewUtils();
                        geocodeSearchViewUtils1.startGeocodeSearch(mContext, item.getAttachment().getOrderFavourableModel().getOnLatitude(), item.getAttachment().getOrderFavourableModel().getOnLongitude(), detailOrderBikeStartLocation);
                        GeocodeSearchViewUtils geocodeSearchViewUtils2 = new GeocodeSearchViewUtils();
                        geocodeSearchViewUtils2.startGeocodeSearch(mContext, item.getAttachment().getOrderFavourableModel().getDownLatitude(), item.getAttachment().getOrderFavourableModel().getDownLongitude(), detailOrderBikeEndLocation);
                        //订单费用
                        detailOrderBikeMoney2.setText(df.format(item.getAttachment().getOrderFavourableModel().getOrderPrice()) + "元");
                        //优惠券费用
                        detailOrderBikeTvCoupon.setText("-" + df.format(item.getAttachment().getOrderFavourableModel().getCouponUserModel().getDenomination()) + "元");
                        if (item.getAttachment().getOrderFavourableModel().getPayType() == 1) {
                            datailOrderBikeZffs.setText("支付宝");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 2) {
                            datailOrderBikeZffs.setText("微信支付");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 3) {
                            datailOrderBikeZffs.setText("个人账户支付");
                        } else if (item.getAttachment().getOrderFavourableModel().getPayType() == 4) {
                            datailOrderBikeZffs.setText("企业账户支付");
                        }
                        //碳排放
                        detailOrderBikeTan.setText(item.getAttachment().getOrderFavourableModel().getCarbonEmission() + "");
                        //隐藏
                        if (item.getAttachment().getOrderFavourableModel().getStatus() == 102 || item.getAttachment().getOrderFavourableModel().getStatus() == 103) {
                            detailOrderBikeIvHaoshi.setVisibility(View.GONE);
                            detailOrderBikeRlHaoshi.setClickable(false);
                            flag = -1;
                        } else {
                            detailOrderBikeIvHaoshi.setVisibility(View.VISIBLE);
                            detailOrderBikeRlHaoshi.setClickable(true);
                            flag = 0;
                        }
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<BikeOrderDetailBean> call, Throwable t) {
                detailProgressBarBikeSettlement.setVisibility(View.GONE);
            }
        });
    }


    @OnClick({R.id.detail_order_bike_iv_haoshi, R.id._detail_order_bike_return, R.id.detail_order_bike_phone, R.id.detail_order_bike_rl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id._detail_order_bike_return:
                finish();
                break;
            case R.id.detail_order_bike_phone:
                final KuaituDialog kuaituDialog2 = new KuaituDialog(mContext);
                kuaituDialog2.setTitle(getString(R.string.dialogTitle));
                kuaituDialog2.setMessage("是否拨打客服电话" + MyApplication.getInstance().getHotLine());
                kuaituDialog2.setYesOnclickListener("确定", new KuaituDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse("tel:" + MyApplication.getInstance().getHotLine()));
                        startActivity(intent);
                        kuaituDialog2.dismiss();
                    }
                });
                kuaituDialog2.setNoOnclickListener("取消", new KuaituDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        kuaituDialog2.dismiss();
                    }
                });
                kuaituDialog2.show();
                break;
            case R.id.detail_order_bike_rl:
            case R.id.detail_order_bike_iv_haoshi:
                if (flag % 2 == 0) {
                    detailOrderBikeRlHaoshi.setVisibility(View.VISIBLE);
                    detailOrderBikeIvHaoshi.setImageResource(R.drawable.icon_details_up);
                    flag++;
                } else if (flag % 2 == 1) {
                    detailOrderBikeRlHaoshi.setVisibility(View.GONE);
                    detailOrderBikeIvHaoshi.setImageResource(R.drawable.icon_details_down);
                    flag++;
                }
                break;
        }
    }
}
